<?php
    include '../connectdb.php';
    if (isset($_GET['del'])) {
        $q="delete from customer where customer_id=".$_GET["customer_id"];
        mysqli_query($link, $q);
        header('Location: main.php?customer=true');
        die;
    }
    if (isset($_POST['action'])) {
        $q = "customer set customer_fname='".$_POST["name"]."', customer_lname='".$_POST["last_name"]."', ";
        $q.= "phone_number='".$_POST["phone"]."', email='".$_POST["email"]."'";
        if ($_POST['action'] == 'add') {
            $q = "insert into ".$q.", password='".$_POST["password"]."'";
        } else { // edit
            $q = "update ".$q." where customer_id=".$_POST["customer_id"];
        }
        mysqli_query($link, $q);
    }
    if (isset($_GET["add"]) || isset($_GET["edit"])) {
        if (isset($_GET["edit"])) {
            $q = "select * from customer where customer_id=".$_GET["customer_id"];
            $result = mysqli_query($link, $q);
            $row = mysqli_fetch_array($result);
            $url = "main.php?customer=true";//&edit=true&customer_id=".$_GET["customer_id"];
            $action = 'edit';
        } else {
            $url = "main.php?customer=true";//&add=true";
            $action = 'add';
        }
?>
<form action="<?php echo $url ?>" method="POST">
<input type="hidden" name="action" value="<?php echo $action ?>" />
<input type="hidden" name="customer_id" value="<?php echo $_GET["customer_id"] ?>" />
<table border="1" width="764">
    <tr>
        <td colspan="2" align="center"><h3>Add/Edit Customer</h3></td>
    </tr>
    <tr>
        <td>name</td>
        <td><input type="text" name="name" value="<?php echo $row["customer_fname"] ?>" /></td>
    </tr>
    <tr>
        <td>last_name</td>
        <td><input type="text" name="last_name" value="<?php echo $row["customer_lname"] ?>" /></td>
    </tr>
    <tr>
        <td>phone</td>
        <td><input type="text" name="phone" value="<?php echo $row["phone_number"] ?>" /></td>
    </tr>
    <tr>
        <td>email</td>
        <td><input type="text" name="email" value="<?php echo $row["email"] ?>" /></td>
    </tr>
    <?php
    if (isset($_GET["edit"])) {
    ?>
    <tr>
        <td>password</td>
        <td><a href="reset_password.php">reset password</a></td>
    </tr>
    <?php
    } else {
    ?>
    <tr>
        <td>password</td>
        <td><input type="text" name="password"></td>
    </tr>
    <?php
    }
    ?>
    <tr>
        <td colspan="2" align="center"><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></td>
    </tr>    
</table>
</form>
<?php
    } else {
?>
<script type="text/javascript">
    function confirm_delete (url) {
        if (confirm("โปรดยืนยันการลบข้อมูล")) {
            location.href = url;
        }
    }
</script>
<table border="1" width="764">
    <tr>
        <td colspan="8" align="center"><h3>Customer Management</h3></td>
    </tr>
    <tr>
        <td colspan="8" align="right"><a href="main.php?customer=true&add=true">+Add</a></td>
    </tr>
    <tr>
        <td>id</td>
        <td>name</td>
        <td>last_name</td>
        <td>phone</td>
        <td>email</td>
        <td>password</td>
        <td>edit</td>
        <td>delete</td>
    </tr>
    <?php
        $q = "select * from customer";
        $result = mysqli_query($link, $q);
        while ($row = mysqli_fetch_array($result)){ 
    ?>
    <tr>
        <td><?php echo $row['customer_id'];?></td>
        <td><?php echo $row['customer_fname'];?></td>
        <td><?php echo $row['customer_lname'];?></td>
        <td><?php echo $row['phone_number'];?></td>
        <td><?php echo $row['email'];?></td>
        <td><?php echo $row['password'];?></td>
        <td><a href="main.php?customer=true&customer_id=<?php echo $row['customer_id'] ?>&edit=true">edit</a></td>
        <td><a href="javascript:confirm_delete('main.php?customer=true&customer_id=<?php echo $row['customer_id'] ?>&del=true');">delete</a></td>
    </tr>
    <?php
        }
    ?>
    <tr>
        <td colspan="8" align="right"><a href="customer_report.php" target="_BLANK">Report</a></td>
    </tr>
</table>
<?php
    }
?>